<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="24" :xs="24">
        <!-- 数据筛选表单 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="100px">
          <!-- 数据筛选表单组件 -->
          <el-form-item label="图片标题" prop="title">
            <el-input v-model="queryParams.title" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="文件名" prop="fileName">
            <el-input v-model="queryParams.fileName" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!-- 数据筛选操作按钮 -->
          <el-form-item>
            <el-button icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="handleAdd">上传图片</el-button>

          </el-form-item>
          <el-form-item label="调整列数">
            <template>
              <el-input-number v-model="col" :min="3" :max="8" @change="handleChange" />
            </template>

          </el-form-item>
        </el-form>
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
          <el-upload :action="uploadApi" :headers="headers" :file-list="fileList" list-type="picture-card" :auto-upload="true">
            <i slot="default" class="el-icon-plus" />
            <div slot="file" slot-scope="{ file }">
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
              <span class="el-upload-list__item-actions">
                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                  <i class="el-icon-zoom-in" />
                </span>
                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
                  <i class="el-icon-download" />
                </span>
                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                  <i class="el-icon-delete" />
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="handleUploadFiles">上传到OSS</el-button>
          </div>
        </el-dialog>
        <div class="waterfall">
          <image-water-fall :data="imageList" :col="col" />
        </div>

        <!-- 数据分页加载 -->
        <el-pagination
          :current-page="pageNum"
          :page-sizes="[10, 20, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加修改oss对话框 -->
  </div>
</template>

<script>
/** 导入axios Api */
import { listOssByPage } from '@/api/system/oss'
import { getToken } from '@/utils/auth'
import ImageWaterFall from '@/components/ImageWaterFall'
export default {
  name: 'Oss',
  components: { ImageWaterFall },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 当前页
      pageNum: 0,
      // 每页数据量
      pageSize: 0,
      // Oss表格数据
      ossList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 数据查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        fileName: null
      },
      // 上传图片数据
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      uploadApi: process.env.VUE_APP_BASE_API + '/oss/upload',
      headers: {
        Authorization: 'Bearer ' + getToken()
      },
      // 展示图片数据
      col: 8,
      imageList: [],
      fileList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询Oss数据列表 */
    getList() {
      this.loading = true
      listOssByPage(this.queryParams).then((response) => {
        this.imageList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    },

    /** 数据编辑对话框取消按钮 */
    cancel() {
      this.open = false
      this.resetForm()
    },

    /** 重置分页 */
    resetQueryParams() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
    },

    /** 数据筛选搜索按钮 */
    handleQuery() {
      this.resetQueryParams()
      this.getList()
      this.$message.success('查询成功')
    },

    /** 数据筛选重置按钮 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.getList()
    },

    /** 处理添加图片按钮 */
    handleAdd() {
      this.open = true
      this.title = '上传图片'
    },

    /** 上传成功时
    uploadSuccess(response) {
      if(response.code === 200) {
        ElMessage({
          type: "success",
          message: response.msg
        })
        this.fileList.push(response.data.fileInfo)
      } else {
        ElMessage({
          type: "error",
          message: response.data.msg
        })
      }
    },**/

    /** 手动上传多个图片 */
    handleUploadFiles() {

    },

    /** 上传图片——删除已添加图片 */
    handleRemove(file) {
      console.log(file)
    },
    /** 上传图片——查看图片 */
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    /** 上传图片——下载图片 */
    handleDownload(file) {
      console.log(file)
    },
    /** 修改每页列数 */
    handleChange(value) {
      this.col = value
    }
  }
}
</script>
